package com.example.demo.service.Impl;


import com.example.demo.bean.Greenhouse;
import com.example.demo.bean.GreenhouseExample;
import com.example.demo.dao.GreenhouseMapper;
import com.example.demo.service.IGreenhouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GreenhouseServiceImpl implements IGreenhouseService {

    @Resource
    private GreenhouseMapper greenhouseMapper;

    @Override
    public List<Greenhouse> query(Long id,String name){
        GreenhouseExample example= new GreenhouseExample();
        if(!StringUtils.isEmpty(id)){
            example.createCriteria().andIdEqualTo(id);
        }
        if(!StringUtils.isEmpty(name)){
            example.createCriteria().andNameLike("%"+name+"%");
        }
        List<Greenhouse>list=greenhouseMapper.selectByExample(example);
        return list;
    }

    @Override
    public void saveOrUpdate(Greenhouse greenhouse){
        if(greenhouse.getId()==null){
            greenhouseMapper.insert(greenhouse);
        }else{
            greenhouseMapper.updateByPrimaryKey(greenhouse);
        }
    }

    @Override
    public void deleteById(Long id){
        greenhouseMapper.deleteByPrimaryKey(id);
    }
}


